home *** CD-ROM | disk | FTP | other *** search
- From: jamshid@io.com (Jamshid Afshar)
- Message-ID: <4i4ooa$l5d@xanadu.io.com>
- X-Original-Date: 12 Mar 1996 15:02:02 -0600
- Path: in2.uu.net!bounce-back
- Date: 13 Mar 96 04:50:53 GMT
- Approved: fjh@cs.mu.oz.au
- Newsgroups: comp.std.c++
- Subject: Constructor vs conversion operator in ambiguity resolution
- Organization: Illuminati Online, Austin, Texas, USA
- Summary: please verify I found a compiler bug
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMUZUO+EDnX0m9pzZAQGZ4AF/XLZRIS6cMV/wl/qTEkUmkxQC2n5pUIzS
- 38mezOhI69eYmt+qDMVY37Ivakng3/b5
- =W1fO
-
- I just want to verify that the below code is an ambiguity error. VC++
- 4.0 accepts the code. The rules haven't changed recently in this
- regard, have they? Btw, g++ 2.6.3 also accepts the code, but it calls
- operator<<(ostream&, const char*).
-
- // VC++ should be flagging the "cout << cs;" line below as an
- // ambiguity error because converting a CString to a "const char*" is
- // an equivalent match to using it to construct a temporary Foo. But,
- // VC++ silently accepts and chooses to construct a temporary Foo.
- // A workaround is to define an operator<<(ostream&,const CString&),
- // which really should be defined by MFC anyway.
-
- #include <iostream.h> // defines operator<<( ostream&, const char* )
-
- class CString {
- const char* _s;
- public:
- CString( const char* s ) : _s(s) {} // obviously just for demonstration
- operator const char*() const { return _s; }
- };
-
- class Foo {
- public:
- Foo( const CString& ) {}
- friend ostream& operator<<( ostream& os, const Foo& f )
- { return os << "This is a Foo." << endl; }
- };
-
- int main() {
- CString cs = "this is a CString";
- cout << cs; // error: ambiguous, but VC++ silently accepts using Foo ctor
- cout << endl;
- return 0;
- }
- ---
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-